package com.chaodev.base.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.AdviceDto;
import com.chaodev.base.dto.AdviceReplyDto;
import com.chaodev.base.model.Advice;
import com.chaodev.base.model.AdviceReply;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.AdviceService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/18 8:58
 **/
@RestController
@RequestMapping("advice")
public class AdviceController {
    Logger logger = LoggerFactory.getLogger(AdviceController.class);

    @Autowired
    AdviceService adviceService;
    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    /**
     * 获取建言献策列表分页信息
     */
    @ApiOperation(value = "获取建言献策列表分页", notes = "获取建言献策列表分页信息")
    @PostMapping(value = {"list"})
    public Result list(@RequestBody AdviceDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            PageInfo<Advice> activityNot = adviceService.queryPageList(dto);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取建言献策列表分页", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取建言献策列表分页", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 获取建言献策信息
     */
    @ApiOperation(value = "获取建言献策对象", notes = "获取建言献策信息")
    @GetMapping(value = {"/getById"})
    public Result findById(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Advice tbAdvice=adviceService.findDataById(id);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(tbAdvice);
            logService.logAdd(null, "获取建言献策对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取建言献策对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 保存建言献策信息
     *
     */
    @ApiOperation(value = "保存或修改建言献策对象", notes = "保存或修改建言献策对象")
    @PostMapping(value = {"/save"})
    public Result save(@RequestBody Advice advice, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if(StrUtil.isEmpty(advice.getAveCode())){
                advice.setAveCode(userInfo.getUserId());
            }
            if(StrUtil.isEmpty(advice.getAveName())){
                advice.setAveName(userInfo.getUserName());
            }
            adviceService.saveData(advice);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(advice);
            logService.logAdd(null, "保存或修改建言献策对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存或修改建言献策对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除建言献策信息
     *
     */
    @ApiOperation(value = "删除建言献策对象", notes = "删除建言献策信息")
    @GetMapping(value = {"/delete"})
    public Result delete(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            adviceService.deleteDataById(id);
            result.setCode(0);
            result.setMsg("操作成功");
            logService.logAdd(null, "删除建言献策对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除建言献策对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }


    /**
     * 获取建言献策回复表列表分页
     */
    @ApiOperation(value = "获取建言献策回复表列表分页", notes = "获取建言献策回复表列表分页")
    @PostMapping(value = {"replyList"})
    public Result list(@RequestBody AdviceReplyDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            PageInfo<AdviceReply> activityNot = adviceService.queryReplyPageList(dto);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取建言献策列表分页", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取建言献策列表分页", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 获取建言献策回复表对象
     */
    @ApiOperation(value = "获取建言献策回复表对象", notes = "获取建言献策回复表对象")
    @GetMapping(value = {"/getReplyById"})
    public Result findReplyById(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            AdviceReply tbAdvice=adviceService.findReplyDataById(id);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(tbAdvice);
            logService.logAdd(null, "获取建言献策回复表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取建言献策对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 保存或修改建言献策回复表对象
     *
     */
    @ApiOperation(value = "保存或修改建言献策回复表对象", notes = "保存或修改建言献策回复表对象")
    @PostMapping(value = {"/saveReply"})
    public Result saveReply(@RequestBody AdviceReply adviceReply, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if(StrUtil.isEmpty(adviceReply.getReplyUserCode())){
                adviceReply.setReplyUserCode(userInfo.getUserId());
            }
            if(StrUtil.isEmpty(adviceReply.getReplyUserName())){
                adviceReply.setReplyUserName(userInfo.getUserName());
            }
            adviceService.saveReplyData(adviceReply);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(adviceReply);
            logService.logAdd(null, "保存或修改建言献策回复表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存或修改建言献策回复表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除建言献策回复表对象
     *
     */
    @ApiOperation(value = "删除建言献策回复表对象", notes = "删除建言献策回复表对象")
    @GetMapping(value = {"/deleteReply"})
    public Result deleteReply(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            adviceService.deleteReplyDataById(id);
            result.setCode(0);
            result.setMsg("操作成功");
            logService.logAdd(null, "删除建言献策回复表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除建言献策回复表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

}
